package com.xyzq.webapp.action.ya.report;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.ya.data.Account;
import com.xyzq.model.ya.data.AccountDayFund;
import com.xyzq.model.ya.data.AccountDayMoney;
import com.xyzq.model.ya.data.AccountDayStock;
import com.xyzq.model.ya.data.Staff;
import com.xyzq.model.ya.report.AccountDayAssert;
import com.xyzq.model.ya.report.StaffWithValidAccount;
import com.xyzq.service.ya.AccountDayFundManager;
import com.xyzq.service.ya.AccountDayMoneyManager;
import com.xyzq.service.ya.AccountDayStockManager;
import com.xyzq.service.ya.AccountManager;
import com.xyzq.service.ya.StaffManager;
import com.xyzq.webapp.action.BaseAction;

public class StaffValidAccountReportAction extends BaseAction implements
		Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2132275914094275878L;

	private Date start;
	private Date end;
	private Date valueDay;
	private List<StaffWithValidAccount> staffWithValidAccountList;
	private List<AccountDayAssert> accountDayAssertList;
	private List<Account> accountList;
	private List<AccountDayMoney> dayMoneys;
	private List<AccountDayFund> dayFunds;
	private List<AccountDayStock> dayStocks;
	private AccountManager accountManager;
	private AccountDayFundManager accountDayFundManager;
	private AccountDayMoneyManager accountDayMoneyManager;
	private AccountDayStockManager accountDayStockManager;
	private StaffManager staffManager;

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		List<Staff> staffList = this.staffManager.getAll();
		accountDayAssertList = new ArrayList<AccountDayAssert>();
		this.staffWithValidAccountList = new ArrayList<StaffWithValidAccount>();
		for (Staff staff : staffList) {
			StaffWithValidAccount swva = new StaffWithValidAccount(staff);
			this.staffWithValidAccountList.add(swva);
		}
	}

	public String execute() {
		this.accountList = this.accountManager.findShAccountByDate(start, end);
		this.dayMoneys = this.accountDayMoneyManager.findWithValueDay(valueDay);
		this.dayFunds = this.accountDayFundManager.findWithValueDay(valueDay);
		this.dayStocks = this.accountDayStockManager.findWithValueDay(valueDay);
		for (Account account : accountList) {
			Boolean valid1W = false;
			Boolean valid5W = false;
			Double moneySum = new Double(0);
			Double fundSum = new Double(0);
			Double stockSum = new Double(0);
			for (AccountDayMoney adm : this.dayMoneys) {
				if (account.equals(adm.getAccount())) {
					if (adm.getType().equals("美元")) {
						moneySum = moneySum + adm.getMoney() * 6;
					} else if (adm.getType().equals("港币")) {
						moneySum = moneySum + adm.getMoney() * 0.8;
					} else {
						moneySum = moneySum + adm.getMoney();
					}
				}
			}
			for (AccountDayFund adf : this.dayFunds) {
				if (account.equals(adf.getAccount())) {
					fundSum = fundSum + adf.getWorth();
				}
			}
			for (AccountDayStock ads : this.dayStocks) {
				if (account.equals(ads.getAccount())) {
					stockSum = stockSum + ads.getWorth();
				}
			}
			AccountDayAssert ada = new AccountDayAssert();
			ada.setAccount(account);
			ada.setCash(moneySum);
			ada.setFund(fundSum);
			ada.setStock(stockSum);
			ada.setValueDay(valueDay);
			this.accountDayAssertList.add(ada);
			if (fundSum >= 50000 || stockSum >= 50000) {
				valid5W = true;
			} else if (fundSum >= 20000 || stockSum >= 10000) {
				valid1W = true;
			}
			for (StaffWithValidAccount swva : this.staffWithValidAccountList) {
				if (swva.getStaff().equals(account.getStaff())) {
					if (valid1W) {
						swva.setValidAccount(swva.getValidAccount() + 1);
					} else if (valid5W) {
						swva.setValidAccount5W(swva.getValidAccount5W() + 1);
					}
				}
			}
		}
		this.logUserAction("StaffValidAccountReportAction", "query:"
				+ this.start + "<->" + this.end + " in the " + this.valueDay);
		return SUCCESS;
	}

	public Date getStart() {
		return start;
	}

	public Date getEnd() {
		return end;
	}

	public Date getValueDay() {
		return valueDay;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public void setValueDay(Date valueDay) {
		this.valueDay = valueDay;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public void setAccountDayAssertList(
			List<AccountDayAssert> accountDayAssertList) {
		this.accountDayAssertList = accountDayAssertList;
	}

	public List<AccountDayAssert> getAccountDayAssertList() {
		return accountDayAssertList;
	}

	public void setStaffWithValidAccountList(
			List<StaffWithValidAccount> staffWithValidAccountList) {
		this.staffWithValidAccountList = staffWithValidAccountList;
	}

	public List<StaffWithValidAccount> getStaffWithValidAccountList() {
		return staffWithValidAccountList;
	}

	public List<AccountDayMoney> getDayMoneys() {
		return dayMoneys;
	}

	public List<AccountDayFund> getDayFunds() {
		return dayFunds;
	}

	public List<AccountDayStock> getDayStocks() {
		return dayStocks;
	}

	public AccountDayFundManager getAccountDayFundManager() {
		return accountDayFundManager;
	}

	public AccountDayMoneyManager getAccountDayMoneyManager() {
		return accountDayMoneyManager;
	}

	public AccountDayStockManager getAccountDayStockManager() {
		return accountDayStockManager;
	}

	public void setDayMoneys(List<AccountDayMoney> dayMoneys) {
		this.dayMoneys = dayMoneys;
	}

	public void setDayFunds(List<AccountDayFund> dayFunds) {
		this.dayFunds = dayFunds;
	}

	public void setDayStocks(List<AccountDayStock> dayStocks) {
		this.dayStocks = dayStocks;
	}

	public void setAccountDayFundManager(
			AccountDayFundManager accountDayFundManager) {
		this.accountDayFundManager = accountDayFundManager;
	}

	public void setAccountDayMoneyManager(
			AccountDayMoneyManager accountDayMoneyManager) {
		this.accountDayMoneyManager = accountDayMoneyManager;
	}

	public void setAccountDayStockManager(
			AccountDayStockManager accountDayStockManager) {
		this.accountDayStockManager = accountDayStockManager;
	}

	public StaffManager getStaffManager() {
		return staffManager;
	}

	public void setStaffManager(StaffManager staffManager) {
		this.staffManager = staffManager;
	}

}
